package com.panda.cn.config.ReturnValue;

import cn.hutool.json.JSONNull;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

/**
 * 自定义JSON序列化类
 * 解决问题
 * No serializer found for class cn.hutool.json.JSONNull and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ；
 * 大致意思就是：被转换的对象中存在有属性值为空的存在，导致序列化异常
 *
 * @author Yanmh
 * @date 2022/10/18
 */
@JsonComponent
public class JsonNullSerizlizer extends JsonSerializer<JSONNull> {
    @Override
    public void serialize(JSONNull jsonNull, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        jsonGenerator.writeNull();
    }
}
